 function checkBlocks() {
    isValidBlockTime();
    isSeatLimitReached();
 }
 
 function getReservationListAndRedirect() {
    if (false == $("#reservationListDateForm").validate().element( "#reservation_list_date" )) {
        return false;
    }
     
    var dateObj = $('#reservation_list_date').datepicker('getDate');

    var month = dateObj.getMonth() + 1;
    var day = dateObj.getDate();

    if (month < 10)
        month = "0" + month;
    if (day < 10)
        day = "0" + day;

    var date_string = dateObj.getFullYear() + "-" + month + "-" + day;

    document.location = '/reservation/list/' + date_string;
 }

$(document).ready(function() {
    $("#reservation_backoffice_date").mask("99/99/9999");
    $("#reservation_list_date").mask("99/99/9999");
    $("#reservation_guest_date").mask("99/99/9999");
    
    if (1 == $('#backoffice_form').length) {
        $('#backoffice_form').validate();
        $("#reservation_backoffice_date").rules("add", { dateITA: true });
    }
    
    if (1 == $('#guest_form').length) {
        $('#guest_form').validate();
        $("#reservation_guest_date").rules("add", { dateITA: true });
    }
    
    if (1 == $('#reservationListDateForm').length) {
        $('#reservationListDateForm').validate();
        $("#reservation_list_date").rules("add", { dateITA: true });
        
        $('#reservation_list_date').bind('keypress', function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) { //Enter keycode
                getReservationListAndRedirect();
                return false;
            }
        });
    }
    
    /* set bg of page to color the page according to the style in ui-css*/
    $('body').addClass("yvo-body-content");
    $('html').addClass("yvo-body-content");
	
    /* rest of the scripts */
    set_datepicker('#reservation_backoffice_date');
    set_datepicker('#reservation_guest_date');
    set_datepicker('#reservation_list_date');
    
    $('#reservation_list_date').datepicker('option', 'onSelect', function() {
        getReservationListAndRedirect();
    });
        
    $('#reservation_list_date').blur(function() {
        getReservationListAndRedirect();
    });

    $('#backoffice_submit').click(function(event) {
        event.preventDefault();
        if (isValidGroupMenuContext($('#reservation_backoffice_person_no').val()))

        $('#backoffice_form').submit();
    });

    $('#backoffice_update_submit').click(function(event) {
        event.preventDefault();
        if (isValidGroupMenuContext($('#reservation_backoffice_person_no').val()))
            $('#backoffice_update_form').submit();
    });

    $('#backoffice_delete').click(function(event) {
        event.preventDefault();
        var url = $(this).attr('href');

        jConfirm('Wilt u deze reservering afzeggen?', 'Reservering afzeggen', function(response) {
            if (response) {
                $.ajax({
                    type: "POST",
                    url: url,
                    success: function (response) {
                        document.location = response;
                    }
                });
            }
        });
    });
    
    $(".reservation_large_div").attr({scrollTop: $(".reservation_large_div").attr("scrollHeight")});

    $('#restaurant_select').change(function() {
        set_restaurant($(this).val());
    });

    
    $('#reservation_backoffice_person_no, #reservation_guest_person_no').blur(function() {
        var person_no = $(this).val();
        
        $('#group_menu_list_tr').hide();
        $('#group_menu_message').hide();

        if (isValidNumberOfPersons(person_no)) {
            if (isGroupReservation(person_no)) {
                $('#group_menu_list_tr').show();
                show_group_menu_message();
            }
        }
        
        /* also check seat availability */
        checkBlocks();
    });
    
    $('#reservation_backoffice_time_hour, #reservation_backoffice_time_minute,\n\
       #reservation_guest_time_hour, #reservation_guest_time_minute').blur(function() {
       checkBlocks();
    });
    
    $('#reservation_guest_date').datepicker('option', 'onSelect', function() {
    	checkBlocks();
    });
        
    $('#reservation_backoffice_date').datepicker('option', 'onSelect', function() {
    	checkBlocks();
    });
    
    $("#reservation_backoffice_date").datepicker('option', 'onClose', function() { 
        $(this).valid(); 
    });
    
    $("#reservation_guest_date").datepicker('option', 'onClose', function() { 
        $(this).valid(); 
    });
    
    $('#reservation_guest_date').blur(function() {
    	checkBlocks();
    });
    
    $('#reservation_backoffice_date').blur(function() {
    	checkBlocks();
    });

    $("strong[id^='ok_']").live('click', function(event) {
        event.preventDefault();

        var info = $(this).attr("id").replace('ok_', '');
        var reservation_id = info.substr(0, info.indexOf('_'));
        var type = info.substr(info.indexOf('_') + 1);
        var message = $('#console_message_' + reservation_id).html();
        
        log_console_message(reservation_id, type, message);
    });

    /* guest */
    $('#guest_submit').click(function(event) {
        $(this).hide();
        event.preventDefault();
//        if (isValidGroupMenuContext($('#reservation_backoffice_person_no').val()))
        $('#guest_form').submit();
    });
    
    
    /* show special date block 
     * HV 20-6
     * v1.0
     * */
    $('#reservation_guest_specialdateblock_btn').click(function(event) {
    	event.preventDefault();
    	$('#reservation_guest_specialdateblock').toggleClass('noshow');
    	$('#reservation_guest_specialdateblock_btn span').toggleClass('ui-icon-circle-triangle-n');
    });
    
    
    /* show special requests blocks
     * HV 20-6
     * v1.0
     */
    $('#reservation_guest_specialrequestsblock_btn').click(function(event) {
    	event.preventDefault();
    	$('#reservation_guest_dietrequestblock').toggleClass('noshow');
    	$('#reservation_guest_menurequestblock').toggleClass('noshow');
    	$('#reservation_guest_specialrequestsblock_btn span').toggleClass('ui-icon-circle-triangle-n');
    	
    });
    
    //DEPRECATED ?!
    /* show menu dialog */
//    $("#show_menu").live('click', function(event) {
//        event.preventDefault();
//        var selected_value = $("input[name=group_menu]:checked").val();
//
//        $.ajax({
//            type: "POST",
//            url: '/reservation/show-menu',
//            data: { id: selected_value },
//            success: function () {
//
//            }
//        });
//    });
});